package multiprocessor;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Semaphore;

//todo use other method
public class BuildingH2o1117 {
    Semaphore semaphoreO = new Semaphore(0);
    Semaphore semaphoreH = new Semaphore(2);

    public BuildingH2o1117() {

    }

    public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
        semaphoreH.acquire();
        // releaseHydrogen.run() outputs "H". Do not change or remove this line.
        releaseHydrogen.run();

        if(semaphoreH.availablePermits()==0){
            semaphoreO.release();
        }
    }

    public void oxygen(Runnable releaseOxygen) throws InterruptedException {
        semaphoreO.acquire();
        // releaseOxygen.run() outputs "O". Do not change or remove this line.
        releaseOxygen.run();

        semaphoreH.release(2);
    }
}
